import Opt from '@/components/dt/info/Opt.js'
import businessCommonMgr from '@/biz/businessReport/businessCommon'
import projectMgr from '@/biz/businessReport/project'
import dt from '@dt/dt'
import dateOpt from '@/biz/option/date.js'
import ruleOpt from '@/biz/option/rule.js'
import tool from '@/common/tool.js'
import houseOpt from '@/biz/option/house.js'

let _this = null
let companys = []
let filterCompany = []
let developer = []
let citys = []
let filterCitys = []
class SearchOpt extends Opt {

  constructor(vm) {
    _this = vm
    super()
    this.build()
  }
  disabledDate(date) {
    let nowTime = new Date();
    let month = nowTime.getMonth() + 1;
    let year = nowTime.getFullYear();
    if (month - 1 == 0) {
      year = year - 1;
      month = 12;
    }
    return date && date.valueOf() > (new Date(`${year}-${month}`));
  }
  filter(val, arr, type) {
    if (!val) return arr
    let filterArr = []
    arr.forEach(item => {
      let tmp = item.children.filter((itemC) => {
        if (itemC[type].indexOf(val) >= 0) {
          return itemC
        }
      })
      if (tmp.length > 0) {
        item.children = tmp
        filterArr.push(item)
      }
    });
    return filterArr
  }
  getCompany() {
    let opt = {
      title: '区域筛选',
      key: 'companyIds',
      xl: 6,
      placeholder: '全部',
      tree: {
        options: companys,
        searchBtn: true,
        searchFun: (val) => {
          filterCompany = tool.deepClone(companys)
          opt.tree.options = this.filter(val, filterCompany, 'title')
        }
      }
    }
    let params = {
      isTwoLevel: true,
    }
    businessCommonMgr
      .getOrganizationTree(params)
      .then((res) => {
        companys = res
        filterCompany = res
        opt.tree.options = companys
      })
    return opt
  }
  getCity() {
    let opt = {
      title: '城市选择',
      key: 'cityIds',
      xl: 6,
      placeholder: '全部',
      tree: {
        options: citys,
        searchBtn: true,
        searchFun: (val) => {
          filterCitys = tool.deepClone(citys)
          console.log(this.filter(val, filterCitys, 'title'))
          opt.tree.options = this.filter(val, filterCitys, 'title')
        }
      }
    }
    projectMgr
      .reportOptionRegion()
      .then((res) => {
        citys = res || []
        filterCitys = res || []
        opt.tree.options = citys
      })
    return opt
  }

  getDeveloper(value = '', flag) {
    let opt = {
      title: '开发商简称',
      key: 'developerAbbreviations',
      xl: 5,
      placeholder: '全部',
      tree: {
        options: developer,
        searchBtn: true,
        searchFun: (val) => {
          this.getDeveloper(val, 1)
        }
      }
    }

    let formData = new FormData()
    formData.append('name', value)

    businessCommonMgr
      .getListDeveloperAbbreviation(formData)
      .then((res) => {
        developer = res
        if (!flag) {
          opt.tree.options = developer
        } else {
          this.all[11].tree.options = developer
        }
      })
    return opt
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }
  createAll() {
    this.all = [
      this.getCompany(),
      {
        key: 'queryDate',
        title: '统计日期',
        xl: 6,
        placeholder: '请选择统计日期',
        date: {
          type: 'month',
          clearable: false,
          disabled: (date) => this.disabledDate(date)
        },
      },
      this.getCity(),
      {
        key: 'enterDate',
        title: '项目进场日期',
        xl: 6,
        dtTime: {
          clearable: true,
        },
        rules: [
          ruleOpt.timeInterval(2),
          ruleOpt.check('不支持选择单边日期', (value) => {
            console.log(value, '11111')
            return value[0] == '' ^ value[1] == ''
          })
        ]
      },
      {
        key: 'exitDate',
        title: '实际撤场日期',
        xl: 6,
        dtTime: { clearable: true },
        rules: [
          ruleOpt.timeInterval(2),
          ruleOpt.check('不支持选择单边日期', (value) => {
            return value[0] == '' ^ value[1] == ''
          })
        ]
      },
      houseOpt.create({
        xl: 5,
        to: (info) => {
          console.log(info)
          return {
            buildingId: info.housesId || ''
          }
        }
      }),
      {
        title: '案场状态',
        placeholder: '全部',
        key: 'buildingStatus',
        select: {
          options: businessCommonMgr.getBuildStatus(),
          multiple: true,
          maxTagCount: 2
        }
      },
      {
        title: '合作模式',
        placeholder: '全部',
        key: 'coopModes',
        select: {
          options: businessCommonMgr.getCooperationModeAttrSort(),
          multiple: true,
          maxTagCount: 1
        }
      },
      {
        key: 'openDate',
        title: '预计开盘日期',
        xl: 6,
        dtTime: { clearable: true },
        rules: [
          ruleOpt.timeInterval(2),
          ruleOpt.check('不支持选择单边日期', (value) => {
            return value[0] == '' ^ value[1] == ''
          })
        ]
      },
      {
        title: '代理方式',
        placeholder: '全部',
        xl: 5,
        key: 'salesModes',
        select: {
          options: dt.dictInfo().salesModel,
          multiple: true,
          maxTagCount: 1
        }
      },
      {
        title: '产品类型',
        placeholder: '全部',
        key: 'productTypes',
        xl: 5,
        select: {
          options: businessCommonMgr.getProductType(),
          multiple: true,
          maxTagCount: 1
        }
      },
      this.getDeveloper('', 0),
    ]
  }
}

export default SearchOpt